package com.mce.nebula.entity;

import com.mce.nebula.common.BaseEntity;
import com.mce.nebula.enums.UpgradeStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 云市服务升级记录实体，对应Python的ServiceUpgradeRecord模型
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "service_upgrade_record")
public class ServiceUpgradeRecord extends BaseEntity {
    
    /**
     * 升级类型枚举
     */
    public enum UpgradeType {
        UPGRADE("upgrade", "升级"),
        ADD("add", "添加");
        
        private final String value;
        private final String description;
        
        UpgradeType(String value, String description) {
            this.value = value;
            this.description = description;
        }
        
        public String getValue() {
            return value;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "app_upgrade_record_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private AppUpgradeRecord appUpgradeRecord;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_id", referencedColumnName = "service_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private TenantService service;
    
    @Column(name = "service_cname", length = 100, nullable = false)
    private String serviceCname;
    
    @Column(name = "upgrade_type", length = 20)
    private String upgradeType = UpgradeType.UPGRADE.getValue();
    
    @Column(name = "event_id", length = 32)
    private String eventId;
    
    @Column(name = "update_desc", columnDefinition = "TEXT", nullable = false)
    private String update;
    
    @Column(name = "status", nullable = false)
    private Integer status = UpgradeStatus.NOT.getValue();
    
    /**
     * 判断是否已完成
     */
    public boolean isFinished() {
        return this.status != UpgradeStatus.NOT.getValue() && 
               this.status != UpgradeStatus.UPGRADING.getValue() && 
               this.status != UpgradeStatus.ROLLING.getValue();
    }
}